package com.basics.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basics.api.request.login.LoginRequest;
import com.basics.api.request.login.PhoneLoginRequest;
import com.basics.api.response.LoginData;
import com.basics.api.service.LoginApiService;
import com.basics.channel.entity.CyChannel;
import com.basics.channel.service.CyChannelGroupUserService;
import com.basics.channel.service.CyChannelService;
import com.basics.framework.web.domain.RestResult;
import com.basics.organization.entity.CyOrganizationGroup;
import com.basics.organization.entity.CyOrganizationUser;
import com.basics.organization.service.CyOrganizationGroupService;
import com.basics.organization.service.CyOrganizationUserService;
import com.basics.user.entity.CyUser;
import com.basics.user.service.CyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/login/")
@Api(tags = "登录模块")
public class LoginApiController extends BaseApiController{

    @Autowired
    private LoginApiService loginApiService;

    @ApiOperation(value = "app获取验证码")
    @GetMapping("/getPhoneCode")
    public RestResult<LoginData> getPhoneCode(@RequestParam(value = "phone")String phone){
        loginApiService.getPhoneCode(phone);
        return RestResult.success();
    }
    @ApiOperation(value = "app手机号验证码登录")
    @PostMapping("/phoneLogin")
    public RestResult<LoginData> phoneLogin(@RequestBody @Valid PhoneLoginRequest req){
        return RestResult.success(loginApiService.phoneLogin(req));
    }

    @ApiOperation(value = "app授权微信登录")
    @PostMapping("/appLogin")
    public RestResult<LoginData> appLogin(@RequestBody @Valid LoginRequest req){
        return RestResult.success(loginApiService.appLogin(req.getCode()));
    }


    @Autowired
    private CyOrganizationUserService cyOrganizationUserService;
    @Autowired
    private CyChannelGroupUserService cyChannelGroupUserService;
    @Autowired
    private CyChannelService cyChannelService;
    @GetMapping("/joinOrganizationByUserId")
    @ApiOperation("加入组织")
    public RestResult<String> joinOrganizationByUserId(@RequestParam(value = "organizationId")Long organizationId,
                                                       @RequestParam(value = "userId") Long userId){
//        CyOrganizationUser cyOrganizationUser = cyOrganizationUserService.getByUserId(userId,organizationId);
//        if(cyOrganizationUser!=null){
//            return RestResult.error("该用户已加入组织");
//        }
//        CyUser user = userService.getById(userId);
//        if(user==null){
//            return RestResult.error("该用户不存在");
//        }
//
//        cyOrganizationUser = new CyOrganizationUser();
//        cyOrganizationUser.setOrganizationId(organizationId);
//        cyOrganizationUser.setUserId(userId);
//        cyOrganizationUser.setDefaultFlag(false);
//        cyOrganizationUserService.save(cyOrganizationUser);
//
//        List<CyChannel> channelList = cyChannelService.list(new QueryWrapper<CyChannel>().eq("organizationId",organizationId));
//        if(CollectionUtils.isNotEmpty(channelList)){
//            channelList.forEach(cyChannel -> {
//                cyChannelGroupUserService.addAllUserByOrganizationId(organizationId,cyChannel.getId());
//            });
//        }
        return RestResult.success();
    }

    @Autowired
    private CyUserService userService;
    @Autowired
    protected CyOrganizationGroupService cyOrganizationGroupService;

    @GetMapping("/createUser")
    @ApiOperation("创建账号")
    public RestResult<String> createUser(@RequestParam(value = "phone")String phone){
//        CyUser user = userService.getByPhone(phone);
//        if(user!=null){
//            return RestResult.error("该手机号已被占用");
//        }
//        user = new CyUser();
//        user.setNickName("测试账号");
//        user.setSource("test");
//        user.setAvatar("https://oss.dimoclub.com/avatar/微信图片_20240409171829_1712654320596.jpg");
//        user.setPhone(phone);
//        userService.save(user);
//        List<String> mjList = Arrays.asList(mj.split(","));
//        int idx = 0;
//        long start = 11013251236l;
//        CyOrganizationGroup group = cyOrganizationGroupService.getDefaultGroup(2222l);
//        for (String s : mjList) {
//            String nick = s.split("-")[0];
//            String avatar = s.split("-")[1];
//            CyUser user = new CyUser();
//            user.setNickName(nick);
//            user.setSource("mj");
//            user.setAvatar(avatar);
//            user.setPhone(String.valueOf(start+idx));
//            userService.save(user);
//            idx++;
//            CyOrganizationUser cyOrganizationUser = new CyOrganizationUser();
//            cyOrganizationUser.setOrganizationId(2222l);
//            cyOrganizationUser.setUserId(user.getId());
//            if(group!=null){
//                cyOrganizationUser.setGroupId(group.getId());
//            }
//            cyOrganizationUser.setDefaultFlag(false);
//            cyOrganizationUserService.save(cyOrganizationUser);
//        }

        List<CyUser> cyUsers = userService.list(new QueryWrapper<CyUser>().eq("source","mj"));
        cyUsers.forEach(cyUser -> {
            cyChannelGroupUserService.addAllChannelByOrganizationId(2222l,cyUser.getId());
        });
        return RestResult.success();
    }
}
